﻿/*
 * @author: wizardc
 */

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

namespace Dou.URP
{
    /// <summary>
    /// 支持 MonoBehaviour 事件周期的 Volume 接口
    /// </summary>
    public interface IVolumeBehaviour
    {
        public void OnActivate();
        
        public void OnUpdate();
        
        public void OnDeactive();
    }
    
    /// <summary>
    /// 将该组件挂到同 Volume 组件一起的对象上，可以使 Volume 支持 MonoBehaviour 的事件周期
    /// </summary>
    public class VolumeBehaviour : MonoBehaviour
    {
        private readonly List<IVolumeBehaviour> _volumeBehaviourList = new List<IVolumeBehaviour>();

        private void Awake() {
            var volume = GetComponent<Volume>();
            if (volume != null)
            {
                var list = volume.profile.components;
                foreach (var v in list)
                {
                    if (v.active && v is IVolumeBehaviour)
                    {
                        _volumeBehaviourList.Add(v as IVolumeBehaviour);
                    }
                }
            }
        }
        
        private void OnEnable()
        {
            foreach (var volume in _volumeBehaviourList)
            {
                volume.OnActivate();
            }
        }
        
        private void LateUpdate()
        {
            foreach (var volume in _volumeBehaviourList)
            {
                volume.OnUpdate();
            }
        }
        
        private void OnDisable()
        {
            foreach (var volume in _volumeBehaviourList)
            {
                volume.OnDeactive();
            }
        }
    }
}
